package com.anbao.newanbao;

import java.util.List;

import com.anbao.action.ActivityManagerAction;
import com.anbao.dataBean.MessageBaseBean;
import com.anbao.service.BaseMessgeService;
import com.anbao.service.MessService;
import com.anbao.util.CommonData;
import com.anbao.util.MessageCommonData;
import com.anbao.util.NumberUtil;
import com.anbao.util.SystemUtil;
import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapEditText;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
/**
 * 修改密码界面
 * @author tiansl
 *
 */
public class UpdataUserPwdActivity extends Activity {
	private ImageView left_img;

	private BootstrapEditText oldpwd;
	private BootstrapEditText newpwd1;
	private BootstrapEditText newpwd2;
	private BootstrapButton updatePwdBtn;
	private updatePwdServiceImpl service;
	
	private int  resultState = -1;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_updata_user_pwd);
		service  = new updatePwdServiceImpl();
		//注册广播接收器
		service.regMessageResult();
		// 将当前界面加入到系统管理中
		ActivityManagerAction.getScreenManager().pushActivity(this);
		iniView();
	}

	private void iniView() {

		left_img = (ImageView) findViewById(R.id.left_img);
		left_img.setOnClickListener(new OnClickListener() {

			@Override
				// Intent intent = new
			public void onClick(View arg0) {
				// Intent(UpdataUserPwdActivity.this,SettingActivity.class);
				// startActivity(intent);
				UpdataUserPwdActivity.this.finish();
			}
		});
		oldpwd = (BootstrapEditText) findViewById(R.id.oldpwd);
		newpwd1 = (BootstrapEditText) findViewById(R.id.newpwd);
		newpwd2 = (BootstrapEditText) findViewById(R.id.newpwd2);
		newpwd2 = (BootstrapEditText) findViewById(R.id.newpwd2);
		updatePwdBtn = (BootstrapButton) findViewById(R.id.updatePwdBtn);
		
		updatePwdBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				service.updatePwd();
			}
		});
	}



	class updatePwdServiceImpl extends BaseMessgeService implements MessService {

		
		public void updatePwd() {
			
			String oldPwdVal = oldpwd.getText().toString();
			String newPwdVal1 = newpwd1.getText().toString();
			String newPwdVal2 = newpwd2.getText().toString();
			
			if(checkpwd(newPwdVal1,newPwdVal2,oldPwdVal)){
				
				sendMessage(getMessageData(oldPwdVal,newPwdVal1));
			}
			
		}
		
		private byte[] getMessageData(String oldPwdVal, String newPwdVal1) {
			
			MessageBaseBean bean = new MessageBaseBean();
			
			
			bean.messageVersion = MessageCommonData.headCmd_commData.CMDVERSION;
			bean.messageType= MessageCommonData.headCmd_req.UPDATE_PWD_REQ;
			bean.usrType = MessageCommonData.usrType.MOBILE_USR;
			bean.messageNumber= 0;
			bean.usrid = SystemUtil.usrId;
	
			List<Byte> list = getHeadList(bean);
			
			byte[] oldPwdArray =  NumberUtil.getMD5Byte(oldPwdVal);
			byte[] newPwdArray =  NumberUtil.getMD5Byte(oldPwdVal);
			
			//填充旧密码数据
			for(int i =0 ; i < oldPwdArray.length ; i ++){
				list.add(oldPwdArray[i]);
			}
			//填充新密码数据
			for(int i =0 ; i < newPwdArray.length ; i ++){
				list.add(newPwdArray[i]);
			}
			//设置密码长度
			int size = list.size();
			list.add(0,(byte) ((size+2)/256));
			list.add(0,(byte) ((size+2)%256));
			
			size = list.size();
			
			byte[] data = new byte[size];
			
			
			for(int i=0 ; i <size;i++){
				data[i] = list.get(i);
				
			}
			return data;
		}

		/**
		 * 检查帐号是否合法
		 * @param newPwdVal1
		 * @param newPwdVal2
		 * @param oldPwdVal
		 * @return
		 */
		private boolean checkpwd(String newPwdVal1, String newPwdVal2,
				String oldPwdVal) {
			
			if(newPwdVal1==null||newPwdVal1.equals("")||newPwdVal2==null||newPwdVal2.equals("")||oldPwdVal==null||oldPwdVal.equals("")){
				
				Toast.makeText(UpdataUserPwdActivity.this, "密码不能为空", 0).show();
				return false;
			}
			//这里对输入的密码与现在保存的密码做比较
			if(!oldPwdVal.equals("123")){
				Toast.makeText(UpdataUserPwdActivity.this, "原始密码错误", 0).show();
				return false;
			}
			
			if(!newPwdVal1.equals(newPwdVal2)){
				
				Toast.makeText(UpdataUserPwdActivity.this, "新密码两次输入不一致", 0).show();
				return false;
			}
			
			
			
			
			return true;
		}

		@Override
		public boolean sendMessage(byte[] data) {

			//先将状态设置为初始化
			resultState=-1;
		//通过广播将登陆信息发送给socket服务
		   Intent mIntent = new Intent(CommonData.UPDATEPWDACTION);  
           mIntent.putExtra("data", data);  
           //发送广播  
           UpdataUserPwdActivity.this.sendBroadcast(mIntent); 
			for(int i =0 ; i < 3 ; i++){
				
				if(resultState!=-1){
					return true;
				}else{
					 try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			
		}
			return false;
		}

		@Override
		public void regMessageResult() {

				IntentFilter myIntentFilter = new IntentFilter();  
		        myIntentFilter.addAction(CommonData.UPDATEPWDACTIONRESULT);  
		        //注册广播        
		        UpdataUserPwdActivity.this.registerReceiver(mBroadcastReceiver, myIntentFilter);  
			
		}
		
		
		//广播接收器
		  private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){  
		        @Override  
		        public void onReceive(Context context, Intent intent) {  
		            String action = intent.getAction();  
		            if(action.equals(CommonData.UPDATEPWDACTIONRESULT)){  
		            	byte[] data =  (byte[]) intent.getExtras().get("data");
		            	resultState = data[1]*256+data[0];
		            }  
		        }  
		          
		    };  

	}

}
